<?
include('./header.inc');
include('./database.inc');

    function convert_to_filename ($string) {
     
    $string = str_replace (" ", "_", $string);
    $string = str_replace ("..", ".", $string);
     
    preg_replace ("/[^0-9^a-z^_^.]/", "", $string);
    return $string;
    }

if (!isset($_POST['submitted'])){
	header( 'Location: ./newslide.php' ) ;
	die();
}

if (empty($_POST['name']) || $_POST['name'] == ""){
	notify("error", "Please Give The Slides A Name");
	header( 'Location: ./newslide.php' ) ;
	die();
}

$name = mysql_real_escape_string($_POST['name']);
$folder = convert_to_filename($name);
$query1 = "SELECT * FROM slides WHERE slide_name = '$name'";
$result1 = mysql_query($query1);
$num_rows = mysql_num_rows($result1);
if ($num_rows > 0){
	notify("error", "Slide Name Already Used");
	header( 'Location: ./newslide.php' ) ;
	die();
}

$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension1 = end(explode(".", $_FILES["100x"]["name"]));
$extension2 = end(explode(".", $_FILES["400x"]["name"]));
$extension3 = end(explode(".", $_FILES["1000x"]["name"]));

if ((($_FILES["100x"]["type"] == "image/gif")
|| ($_FILES["100x"]["type"] == "image/jpeg")
|| ($_FILES["100x"]["type"] == "image/png")
|| ($_FILES["100x"]["type"] == "image/pjpeg"))
&& (($_FILES["400x"]["type"] == "image/gif")
|| ($_FILES["400x"]["type"] == "image/jpeg")
|| ($_FILES["400x"]["type"] == "image/png")
|| ($_FILES["400x"]["type"] == "image/pjpeg"))
&&(($_FILES["1000x"]["type"] == "image/gif")
|| ($_FILES["1000x"]["type"] == "image/jpeg")
|| ($_FILES["1000x"]["type"] == "image/png")
|| ($_FILES["1000x"]["type"] == "image/pjpeg"))
&& in_array($extension1, $allowedExts)
&& in_array($extension2, $allowedExts)
&& in_array($extension3, $allowedExts)){
	
} else {
	notify("error", "Invalid Image Uploaded");
	header( 'Location: ./newslide.php' ) ;
	die();
}

if (($_FILES['100x']['error'] > 0) || ($_FILES['400x']['error'] > 0) || ($_FILES['1000x']['error'] > 0)){
	notify("error", "Error Uploading Images");
	header( 'Location: ./newslide.php' ) ;
	die();
}

mkdir("../images/$folder");

move_uploaded_file($_FILES['100x']["tmp_name"], "../images/$folder/100x.$extension1");
move_uploaded_file($_FILES['400x']["tmp_name"], "../images/$folder/400x.$extension2");
move_uploaded_file($_FILES['1000x']["tmp_name"], "../images/$folder/1000x.$extension3");

$notes = mysql_real_escape_string($_POST['notes']);


$query = "INSERT INTO slides (slide_name, 100xfile, 400xfile, 1000xfile, notes, folder) VALUES ('$name', '100x.$extension1','400x.$extension2','1000x.$extension3', '$notes', '$folder')";
$result = mysql_query($query);

notify("success", "Slide \"$name\" Created");
header( 'Location: ./slides.php' ) ;

?>